package com.wjj.sixinone.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wjj.sixinone.common.enums.ExceptionEnum;
import com.wjj.sixinone.common.exception.GlobalException;
import com.wjj.sixinone.common.pojo.dto.InstrumentDTO;
import com.wjj.sixinone.common.pojo.entity.Drug;
import com.wjj.sixinone.common.pojo.entity.Instrument;
import com.wjj.sixinone.common.pojo.excel.DrugExcel;
import com.wjj.sixinone.common.pojo.excel.InstrumentExcel;
import com.wjj.sixinone.common.pojo.vo.InstrumentFrom;
import com.wjj.sixinone.common.pojo.vo.InstrumentVO;
import com.wjj.sixinone.common.util.Result;
import com.wjj.sixinone.handler.ExcelExportHandle;
import com.wjj.sixinone.mapper.InstrumentMapper;
import com.wjj.sixinone.service.InstrumentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wjj.sixinone.service.minio.MinioFileService;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 小王不秃头
 * @since 2024-07-22
 */
@Service
public class InstrumentServiceImpl extends ServiceImpl<InstrumentMapper, Instrument> implements InstrumentService {

    @Autowired
    private InstrumentMapper instrumentMapper;
    @Autowired
    private ExcelExportHandle excelExportHandle;
    @Autowired
    private MinioFileService minioFileService;
    @Override
    public Result selectInstrumentByLimit(InstrumentVO instrumentVO) {
        Integer page = instrumentVO.getPage();
        Integer limit = instrumentVO.getLimit();
        int index = (page - 1) * limit;
        InstrumentDTO instrumentDTO = new InstrumentDTO();
        BeanUtils.copyProperties(instrumentVO,instrumentDTO);
        instrumentDTO.setIndex(index);
        Integer count =instrumentMapper.count(instrumentDTO);

        List<Instrument> list= instrumentMapper.selectInstrumentByLimit(instrumentDTO);
        return Result.ok().put("data",list).put("count",count);
    }

    @Override
    public void uploadExcel(HttpServletResponse response) {
        List<Instrument> list = instrumentMapper.selectList(new LambdaQueryWrapper<Instrument>().select(Instrument::getInstrumentName,
                Instrument::getInstrumentNo, Instrument::getInstrumentPrice,
                Instrument::getInstrumentQuantity, Instrument::getInstrumentRen,
                Instrument::getImg, Instrument::getCreateTime, Instrument::getCreateUser, Instrument::getUpdateTime, Instrument::getUpdateUser));
        List<InstrumentExcel> userExcels = new ArrayList<>();
        for (Instrument instrument :list) {
            InstrumentExcel item =  new InstrumentExcel();

            BeanUtils.copyProperties(instrument, item);
            userExcels.add(item);
        }
        excelExportHandle.export(response, "仪器信息", userExcels, InstrumentExcel.class);

    }

    @Override
    public Result updateInstrumentById(InstrumentFrom instrumentFrom) {
        Instrument instrument = new Instrument();
        BeanUtils.copyProperties(instrumentFrom,instrument);
        if (instrumentMapper.updateById(instrument) < 1) {
            throw new GlobalException(ExceptionEnum.UPDATE_ERROR);

        }
        return Result.ok();
    }

    @Override
    public Result addInstrument(InstrumentFrom instrument) {
        String instrumentNo = instrument.getInstrumentNo();
        if (instrumentMapper.selectCount(new LambdaQueryWrapper<Instrument>().eq(Instrument::getInstrumentNo, instrumentNo)) > 0) {
            throw new GlobalException(ExceptionEnum.INSTRUMENT_NO_EXIST);
        }
        String img = instrument.getImg();
        String url=null;
        if(img != null){
            url = minioFileService.uploadBase64File(img);
        }
        instrument.setImg(url);
        Instrument instrument1 = new Instrument();
        BeanUtils.copyProperties(instrument,instrument1);
        if (instrumentMapper.insert(instrument1) < 1) {
            throw new GlobalException(ExceptionEnum.INSERT_ERROR);
        }
        return Result.ok();
    }

    @Override
    public Result deleteById(Map<String, String> map) {
        String id = map.get("id");
        String img = map.get("img");
        if (img != null){
            minioFileService.deleteFileFromMinio(img);
        }
        if (instrumentMapper.deleteById(id) < 1) {
            throw new GlobalException(ExceptionEnum.DELETE_ERROR);
        }
        return Result.ok();
    }

}
